//
//  SHStautsCell.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/23.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
import SnapKit
class SHStautsCell: UITableViewCell {

    
    var toolBarBottomConstraint: Constraint?
    
    var statusViewModel: SHStatusViewModel?{
        
        didSet{
            //赋值
            originalView.statusViewModel = statusViewModel
            
//            let result = statusViewModel?.status?.retweeted_status
//            print(result)
            
            self.toolBarBottomConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status == nil{
                
             toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                self.toolBarBottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint
             })
                
                retweetView.hidden = true
            }else{
                //先赋值
                retweetView.statusViewModel = statusViewModel
               toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
               })
                
                retweetView.hidden = false
            }
            
            //赋值，都从ViewModel中去取数据
            toolBarView.statusViewModel = statusViewModel
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
//        selectionStyle = .None
        backgroundColor = RGB(237, green: 237, blue: 237)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(contentView)
//            make.height.equalTo(50)
//            make.top.equalTo(contentView).offset(SHStatusCellMargin)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            // 此约束已经在控件中约束了
            // make.height.equalTo(50)
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            
           self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.height.equalTo(35)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            
            make.bottom.equalTo(toolBarView)
        }
    }
    
    
    private lazy var originalView: SHStatusOriginalView = SHStatusOriginalView()
    
    private lazy var retweetView: SHStatusRetweetView = SHStatusRetweetView()
    
    private lazy var toolBarView: SHStatusToolBarView = SHStatusToolBarView()

}
